package com.dnjn.mes.biz.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dnjn.mes.biz.domain.MesWorkpiece;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 工件 零部件 Mapper 接口
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
public interface MesWorkpieceMapper extends BaseMapper<MesWorkpiece> {
    @ResultMap("BaseResultMap")
    @Select("select p.*,a.* from mes_equipment e left join mes_assembly a on a.equipment_id =e.equipment_id " +
            " left join mes_workpiece p on p.assembly_id =a.assembly_id " +
            " where 1=1 and e.equipment_name =#{equipmentName} and a.assembly_name= #{assemblyName} and p.workpiece_name=#{workpieceName}")
    MesWorkpiece queryBy(@Param("equipmentName") String equipmentName, @Param("assemblyName") String assemblyName,
                         @Param("workpieceName") String workpieceName);

    List<MesWorkpiece> selectWorkList(MesWorkpiece domain);

    @Update("update mes_workpiece set time_spent =time_spent+ ${durationHour} where 1=1 and maintain_type=#{maintainType} ")
    void updateTimeSpentByMaintainType(@Param("durationHour") double durationHour, @Param("maintainType") String maintainType);

    List<MesWorkpiece> searchWorkByName(@Param("searchKey") String searchKey,@Param("assemblyId") Long assemblyId);

//    @Select("SELECT MIN(maintain_duration - time_spent) AS maintainDuration,assembly_id AS assemblyId,workpiece_id AS workpieceId FROM mes_workpiece where deleted = '0' and equipment_id=#{equipmentId} ")
    @Select("SELECT (a.maintain_duration - a.time_spent) AS maintainDuration,a.workpiece_id,b.assembly_id FROM mes_workpiece  a LEFT JOIN mes_assembly b ON a.assembly_id=b.assembly_id WHERE a.deleted='0' AND b.deleted='0' and a.equipment_id=#{equipmentId} and b.equipment_id=#{equipmentId} ")
    List<MesWorkpiece> maintenanceCountdown(@Param("equipmentId") Long equipmentId);

    @Update("update mes_workpiece set deleted = '1' where workpiece_id = #{workpieceId}")
    Long deleteWorkById(@Param("workpieceId") Long workpieceId);

    @Update("update mes_workpiece set last_maintain_time = NOW(),time_spent = '0' where 1=1 and workpiece_id =#{workpieceId} ")
    void maintenanceWork(@Param("workpieceId") Long workpieceId);

    @Select("SELECT MAX(ordinal) FROM mes_workpiece")
    Integer getMaxOrdinal();

    @Select("SELECT count(*) FROM mes_workpiece a left join mes_assembly b on a.assembly_id = b.assembly_id where a.workpiece_name = #{workpieceName} and b.assembly_name=#{assemblyName} ")
    long queryByName(@Param("workpieceName") String workpieceName, @Param("assemblyName") String assemblyName);

    @Select("SELECT a.* FROM mes_workpiece a LEFT JOIN mes_assembly b ON a.assembly_id=b.assembly_id WHERE b.equipment_id=#{equipmentId} AND a.deleted = '0' ")
    List<MesWorkpiece> selectListByEquipmentId(@Param("equipmentId") Long equipmentId);
}
